package emailBox

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/emailBox"
	emailBoxReq "github.com/flipped-aurora/gin-vue-admin/server/model/emailBox/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type EmailConfigApi struct {
}

var emailConfigService = service.ServiceGroupApp.EmailboxServiceGroup.EmailConfigService

// CreateEmailConfig 创建EmailConfig
// @Tags EmailConfig
// @Summary 创建EmailConfig
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body emailBox.EmailConfig true "创建EmailConfig"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /emailConfig/createEmailConfig [post]
func (emailConfigApi *EmailConfigApi) CreateEmailConfig(c *gin.Context) {
	var emailConfig emailBox.EmailConfig
	_ = c.ShouldBindJSON(&emailConfig)
	if err := emailConfigService.CreateEmailConfig(emailConfig); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteEmailConfig 删除EmailConfig
// @Tags EmailConfig
// @Summary 删除EmailConfig
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body emailBox.EmailConfig true "删除EmailConfig"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /emailConfig/deleteEmailConfig [delete]
func (emailConfigApi *EmailConfigApi) DeleteEmailConfig(c *gin.Context) {
	var emailConfig emailBox.EmailConfig
	_ = c.ShouldBindJSON(&emailConfig)
	if err := emailConfigService.DeleteEmailConfig(emailConfig); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteEmailConfigByIds 批量删除EmailConfig
// @Tags EmailConfig
// @Summary 批量删除EmailConfig
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除EmailConfig"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /emailConfig/deleteEmailConfigByIds [delete]
func (emailConfigApi *EmailConfigApi) DeleteEmailConfigByIds(c *gin.Context) {
	var IDS request.IdsReq
	_ = c.ShouldBindJSON(&IDS)
	if err := emailConfigService.DeleteEmailConfigByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateEmailConfig 更新EmailConfig
// @Tags EmailConfig
// @Summary 更新EmailConfig
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body emailBox.EmailConfig true "更新EmailConfig"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /emailConfig/updateEmailConfig [put]
func (emailConfigApi *EmailConfigApi) UpdateEmailConfig(c *gin.Context) {
	var emailConfig emailBox.EmailConfig
	_ = c.ShouldBindJSON(&emailConfig)
	if err := emailConfigService.UpdateEmailConfig(emailConfig); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindEmailConfig 用id查询EmailConfig
// @Tags EmailConfig
// @Summary 用id查询EmailConfig
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query emailBox.EmailConfig true "用id查询EmailConfig"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /emailConfig/findEmailConfig [get]
func (emailConfigApi *EmailConfigApi) FindEmailConfig(c *gin.Context) {
	var emailConfig emailBox.EmailConfig
	_ = c.ShouldBindQuery(&emailConfig)
	if reemailConfig, err := emailConfigService.GetEmailConfig(emailConfig.Uuid); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reemailConfig": reemailConfig}, c)
	}
}

// GetEmailConfigList 分页获取EmailConfig列表
// @Tags EmailConfig
// @Summary 分页获取EmailConfig列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query emailBoxReq.EmailConfigSearch true "分页获取EmailConfig列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /emailConfig/getEmailConfigList [get]
func (emailConfigApi *EmailConfigApi) GetEmailConfigList(c *gin.Context) {
	var pageInfo emailBoxReq.EmailConfigSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if list, total, err := emailConfigService.GetEmailConfigInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
